﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class CmsCatchCache
    {
        private readonly KeRui.DAL.CmsCatchCache dal = new KeRui.DAL.CmsCatchCache();

        public int Add(KeRui.Model.CmsCatchCache model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.CmsCatchCache> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.CmsCatchCache> list = new List<KeRui.Model.CmsCatchCache>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.CmsCatchCache item = new KeRui.Model.CmsCatchCache();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    item.Url = dt.Rows[i]["Url"].ToString();
                    item.Title = dt.Rows[i]["Title"].ToString();
                    if (dt.Rows[i]["CatchID"].ToString() != "")
                    {
                        item.CatchID = int.Parse(dt.Rows[i]["CatchID"].ToString());
                    }
                    item.CatchName = dt.Rows[i]["CatchName"].ToString();
                    if (dt.Rows[i]["NodeID"].ToString() != "")
                    {
                        item.NodeID = int.Parse(dt.Rows[i]["NodeID"].ToString());
                    }
                    item.NodeName = dt.Rows[i]["NodeName"].ToString();
                    if (dt.Rows[i]["ContentID"].ToString() != "")
                    {
                        item.ContentID = int.Parse(dt.Rows[i]["ContentID"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public bool Exists(string Title, int NodeID)
        {
            return this.dal.Exists(Title, NodeID);
        }

        public List<KeRui.Model.CmsCatchCache> GetList(int PageSize, int PageIndex, int NoteID, int CatchID, string Title, string StartTime, string EndTime)
        {
            object obj2;
            string strWhere = "";
            if (Title != "")
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "Title like '%" + Utils.SqlStringFormat(Title, 2) + "%'";
            }
            if (NoteID > 0)
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                obj2 = strWhere;
                strWhere = string.Concat(new object[] { obj2, "NoteID =", NoteID, "" });
            }
            if (CatchID > 0)
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                obj2 = strWhere;
                strWhere = string.Concat(new object[] { obj2, "CatchID =", CatchID, "" });
            }
            if ((StartTime != "") & Utils.IsDateTime(StartTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime >='" + Utils.SqlStringFormat(StartTime) + "'";
            }
            if ((EndTime != "") & Utils.IsDateTime(EndTime))
            {
                if (strWhere != "")
                {
                    strWhere = strWhere + " and ";
                }
                strWhere = strWhere + "CreateTime <='" + Utils.SqlStringFormat(EndTime) + "'";
            }
            string strOrder = "id desc";
            DataSet set = new KeRui.DAL.Common().GetList("CmsCatchCache", PageSize, PageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.CmsCatchCache> GetListByCache(int PageSize, int PageIndex, int NoteID, int CatchID, string Title, string StartTime, string EndTime)
        {
            string cacheKey = string.Concat(new object[] { "CmsCatchCacheList-", PageSize, "-", PageIndex, "-", NoteID, "-", CatchID, "-", Title, "-", StartTime, "-", EndTime });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList(PageSize, PageIndex, NoteID, CatchID, Title, StartTime, EndTime);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.CmsCatchCache>) cache;
        }

        public KeRui.Model.CmsCatchCache GetModel(int NodeID, string Url)
        {
            return this.dal.GetModel(NodeID, Url);
        }

        public KeRui.Model.CmsCatchCache GetModelByCache(int NodeID, string Url)
        {
            string cacheKey = string.Concat(new object[] { "CmsCatchCacheModel-", NodeID, "_", Url });
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(NodeID, Url);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.CmsCatchCache) cache;
        }
    }
}

